import React, {Component} from 'react';
import {StoreProvider} from 'iflux2';
import AppStore from './store';

import {Form, Button} from 'antd';

import OrderInfo from './component/order-info';
import BuyList from './component/buy-list';
import OrderInfoFormer from './component/order-info-former';
import PayDetail from './component/pay-detail';

import {routerShape} from 'constant/PropTypes';
import {createLocationDescriptor} from 'util/history';


@StoreProvider(AppStore, {debug: true})
class EditPrice extends Component {
    constructor(props) {
        super(props);

        this.state = {
            loading: false
        }
    }

    //路由跳转
    static contextTypes = {
        router: routerShape
    }


    componentDidMount() {
        this.props.store.queryOrder(this.props.params.tid);
    }


    render() {
        return (
            <div className="order-change-price">
                <OrderInfo/>
                <BuyList/>
                <OrderInfoFormer/>
                <PayDetail/>
                <div className="fixedSubmit">
                    <div className="t-center">
                        <Button type="default" className="pushr" onClick={this.cancel}>取消</Button>
                        <Button type="primary" loading={this.state.loading} onClick={this.save}>保存</Button>
                    </div>
                </div>
            </div>
        )
    }


    /**
     * 保持修改
     */
    save = ()=>{
        const {store} = this.props;

        this.setState({loading: true});
        store.save()
            .then(()=>{
                this.setState({loading: false});
                this._goActivityListPage(`/order/detail/unpay/${store.state().getIn(['order', 'tid'])}`);
            }).catch(()=>this.setState({loading: false}))
    }


    /**
     * 取消修改
     */
    cancel = ()=>{
        const {store} = this.props;
        this._goActivityListPage(`/order/detail/unpay/${store.state().getIn(['order', 'tid'])}`);
    }


    /**
     * 跳转页面
     * @param link
     * @private
     */
    _goActivityListPage(link) {
        this.context.router.replace(createLocationDescriptor(link));
    }

}

export default Form.create()(EditPrice)